from typing import Optional
import datetime

from sqlalchemy import CHAR, DateTime, Integer, JSON, String, text
from sqlalchemy.dialects.mysql import CHAR, INTEGER, TEXT, TINYINT
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from utils.db_util import BaseEx

class Base(DeclarativeBase, BaseEx):
    pass


class AppTest(Base):
    __tablename__ = 'app_test'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    appid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    test_id: Mapped[Optional[int]] = mapped_column(Integer)
    is_deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"))
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))


class Apps(Base):
    __tablename__ = 'apps'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    name: Mapped[Optional[str]] = mapped_column(CHAR(50))
    appid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    admin: Mapped[Optional[str]] = mapped_column(CHAR(20))
    account: Mapped[Optional[str]] = mapped_column(CHAR(50))
    pwd: Mapped[Optional[str]] = mapped_column(CHAR(50))
    source_id: Mapped[Optional[str]] = mapped_column(CHAR(32))
    secret_id: Mapped[Optional[str]] = mapped_column(CHAR(64))
    is_deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"))
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))


class ImageTask(Base):
    __tablename__ = 'image_task'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    appid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    openid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    prompt: Mapped[Optional[str]] = mapped_column(TEXT)
    detail: Mapped[Optional[dict]] = mapped_column(JSON)
    status: Mapped[Optional[str]] = mapped_column(CHAR(15))
    work_id: Mapped[Optional[str]] = mapped_column(CHAR(32))
    send_res: Mapped[Optional[dict]] = mapped_column(JSON)
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))


class Questions(Base):
    __tablename__ = 'questions'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    test_id: Mapped[Optional[int]] = mapped_column(Integer)
    group: Mapped[Optional[str]] = mapped_column(CHAR(50))
    question_title: Mapped[Optional[str]] = mapped_column(String(255))
    options: Mapped[Optional[dict]] = mapped_column(JSON)
    question_type: Mapped[Optional[str]] = mapped_column(CHAR(20))
    checked: Mapped[Optional[str]] = mapped_column(CHAR(10))
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))


class Sessions(Base):
    __tablename__ = 'sessions'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    session_id: Mapped[Optional[str]] = mapped_column(CHAR(64))
    appid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    openid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    test_id: Mapped[Optional[int]] = mapped_column(Integer)
    content: Mapped[Optional[dict]] = mapped_column(JSON)
    is_deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"))
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))


class Tests(Base):
    __tablename__ = 'tests'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    title: Mapped[Optional[str]] = mapped_column(String(100))
    desc: Mapped[Optional[str]] = mapped_column(String(255))
    rule: Mapped[Optional[str]] = mapped_column(TEXT)
    is_deleted: Mapped[Optional[int]] = mapped_column(TINYINT(1), server_default=text("'0'"))
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))


class Users(Base):
    __tablename__ = 'users'

    id: Mapped[int] = mapped_column(INTEGER, primary_key=True)
    appid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    openid: Mapped[Optional[str]] = mapped_column(CHAR(32))
    nickname: Mapped[Optional[str]] = mapped_column(String(100))
    subscribe_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime)
    last_active_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime)
    create_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP'))
    update_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
